<template>
  <div id="app">
    <Particles id="tsparticles" :options="config" />
    <div class="userinfo">
      <vs-tooltip bottom>
        <vs-button icon circle animation-type="rotate" @click="homeClick">
          <i class="bx bx-home"></i>
          <template #animate>
            <i class="bx bxs-home"></i>
          </template>
        </vs-button>
        <template #tooltip>首页</template>
      </vs-tooltip>

      <vs-tooltip bottom v-if="!user">
        <vs-button icon circle animation-type="rotate" @click="loginClick">
          <i class="bx bx-log-in-circle"></i>
          <template #animate>
            <i class="bx bxs-log-in-circle"></i>
          </template>
        </vs-button>
        <template #tooltip>点击登录</template>
      </vs-tooltip>

      <vs-button v-else icon circle danger animation-type="rotate">
        <i class="bx bx-like"></i>
        {{ like }}
        <template #animate>
          <i class="bx bxs-like"></i>
        </template>
      </vs-button>

      <!-- <vs-avatar v-else circle>
        <img :src="'https://ui-avatars.com/api/?name=' + username" alt="" />
      </vs-avatar> -->

      <span>{{ username }}</span>

      <vs-tooltip bottom v-if="user">
        <vs-button
          icon
          circle
          success
          animation-type="rotate"
          @click="loginClick"
        >
          <i class="bx bx-log-out-circle"></i>
          <template #animate>
            <i class="bx bxs-out-in-circle"></i>
          </template>
        </vs-button>
        <template #tooltip>退出登录</template>
      </vs-tooltip>

      <vs-tooltip bottom v-if="!!user && path != '/task'">
        <vs-button icon warn circle animation-type="rotate" @click="taskClick">
          <i class="bx bx-image-add"></i>
          <template #animate>
            <i class="bx bxs-image-add"></i>
          </template>
        </vs-button>
        <template #tooltip>制作自己的作品</template>
      </vs-tooltip>

      <vs-tooltip bottom v-show="!!user" interactivity>
        <vs-button icon circle danger animation-type="rotate">
          <i class="bx bx-upvote"></i>
          <template #animate>
            <i class="bx bxs-upvote"></i>
          </template>
        </vs-button>
        <template #tooltip>
          <div class="content-tooltip" v-if="tasks.length > 0">
            <vs-row justify="space-between" v-for="t in tasks" :key="t._id">
              <vs-col w="2">
                <vs-avatar class="task-avatar" :loading="!t.pass">
                  <img :src="server + t.name" />
                </vs-avatar>
              </vs-col>
              <vs-col w="4" class="task-context">
                {{ t.pass ? "已运算完成" : "等待算力..." }}
              </vs-col>
              <vs-col w="2">
                <vs-button
                  v-if="!t.pass"
                  danger
                  icon
                  @click="deleteClick(t._id)"
                >
                  <i class="bx bx-trash"></i>
                </vs-button>
              </vs-col>
            </vs-row>
          </div>
          <div v-else>没有AI艺术品深度学习任务</div>
        </template>
      </vs-tooltip>
    </div>
    <transition mode="out-in" enter-active-class="animated bounceInDown">
      <router-view />
    </transition>
  </div>
</template>

<script>
// import HelloWorld from "./components/HelloWorld.vue";
// import signin from "./components/signin.vue";
import effect from "./effect.json";
import { mapState } from "vuex";

import { tasks, deletetask } from "@/api/user";

export default {
  name: "App",
  components: {},
  data() {
    return {
      tasks: [],
      path: "",
      server: process.env.VUE_APP_BASE_API + "/images/",
      active: "",
      config: effect,
    };
  },
  computed: mapState(["like", "user", "username", "latest"]),
  watch: {
    "$route.path": function (newVal) {
      this.path = newVal;
    },
    user: async function (val) {
      if (val) {
        const res = await tasks(val);
        if (res.data && res.data.data) this.tasks = res.data.data;
      }
    },
    latest: async function () {
      if (this.user) {
        const res = await tasks(this.user);
        if (res.data && res.data.data) this.tasks = res.data.data;
      }
    },
  },
  methods: {
    test() {},
    async deleteClick(id) {
      const res = await deletetask(this.user, id);
      if (res.data && res.data.status) {
        this.$store.commit("latest", new Date().getTime());
        this.openSuccessful("删除成功");
      }
    },
    taskClick() {
      this.$router.push("/task");
    },
    homeClick() {
      this.$router.push("/");
    },
    loginClick() {
      this.$store.commit("userid", "");
      this.$store.commit("username", "");
      this.$router.push("/login");
    },
    openSuccessful(title) {
      this.$vs.notification({
        progress: "auto",
        icon: "<i class='bx bx-select-multiple' ></i>",
        color: "success",
        position: "top-center",
        title: title,
      });
    },
    async init() {
      if (this.user) {
        const res = await tasks(this.user);
        if (res.data && res.data.data) this.tasks = res.data.data;
      }
    },
  },
  mounted() {
    this.$vs.setTheme("dark");
    // $route.path
    this.init();
  },
};
</script>

<style lang="stylus">
body {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background-color: rgb(24, 25, 28);
  margin: 0;
  padding: 0;
  color: #fff;
}
#app{
  min-height: 100vh;
  width: 100%;
}
.userinfo{
  display: flex;
  align-items: center;
  position: fixed;
  top: 10px;
  left: 10px;
  z-index :999;
  margin: 10px;

  >span{
    margin: 0 15px 2px 15px ;
    color: #fff;
    font-size: 18px;
    font-weight: bold;
  }
}

.userinfo .vs-button--icon i {
    font-size: 30px!important;
}
.content-tooltip{
  width: 20vw;
}

.task-context{
  display: flex;
  align-items: center;
  margin-top: 15px;
}
.task-avatar {
  margin: 5px;
}

@media all and (orientation : portrait){
  .userinfo{
    margin: 5px;
    >span{
      font-size: 16px;
      margin: 0 5px 2px 5px ;
    }
  }
  .content-tooltip{
    width: 45vw;
  }
  .userinfo .vs-button--icon i {
    font-size: 20px!important;
  }
}
</style>
